package com.wangfei.exception;

import com.wangfei.helper.utils.Result;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e)
    {
        String msg = "被统一接管的异常：" + e.getMessage();
        return Result.error(msg);
    }

    @ExceptionHandler(ApiException.class)
    public Result handleException(ApiException e)
    {
        String msg = e.getMessage();
        return Result.error(msg);
    }

    /**
     * 请求参数验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleException(MethodArgumentNotValidException e)
    {
        String msg = e.getAllErrors().get(0).getDefaultMessage();
        return Result.error(msg);
    }
}
